<?php

namespace Gds\VenteBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Gds\VenteBundle\Entity\SessionCaisse;
use Gds\VenteBundle\Form\SessionCaisseType;
use JMS\SecurityExtraBundle\Annotation\Secure;
use Ps\PdfBundle\Annotation\Pdf;

class SessionCaisseController extends Controller {

    /**
     * @Secure(roles="ROLE_ADMIN")
     */
    public function indexAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        if ($this->getUser()->hasRole('ROLE_SUPER_ADMIN')) {
            $dql = "SELECT s FROM GdsVenteBundle:SessionCaisse s WHERE (s.date_fermeture is NULL)";
            $query = $em->createQuery($dql);
        } else {
            $dql = "SELECT s FROM GdsVenteBundle:SessionCaisse s LEFT JOIN s.caisse c WHERE (s.date_fermeture is NULL and c.departement=:departement)";
            $query = $em->createQuery($dql)->setParameters(array('departement' => $this->getUser()->getDepartement()->getId()));
        }

        $paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
                $query, $request->get('page', 1)/* page number */, 10/* limit per page */
        );
        if ($request->get('type')) {
            return $this->render('GdsVenteBundle:SessionCaisse:pageList.html.twig', array('pagination' => $pagination));
        }
        return $this->render('GdsVenteBundle:SessionCaisse:index.html.twig', array('pagination' => $pagination));
    }

    /**
     * @Secure(roles="ROLE_ADMIN")
     */
    public function listAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        if ($this->getUser()->hasRole('ROLE_SUPER_ADMIN')) {
            $dql = "SELECT s FROM GdsVenteBundle:SessionCaisse s WHERE (s.date_fermeture is NULL)";
            $query = $em->createQuery($dql);
        } else {
            $dql = "SELECT s FROM GdsVenteBundle:SessionCaisse s LEFT JOIN s.caisse c WHERE (s.date_fermeture is NULL and c.departement=:departement)";
            $query = $em->createQuery($dql)->setParameters(array('departement' => $this->getUser()->getDepartement()->getId()));
        }

        $paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
                $query, $request->get('page', 1)/* page number */, 10/* limit per page */
        );
        if ($request->get('type')) {
            return $this->render('GdsVenteBundle:SessionCaisse:pageList.html.twig', array('pagination' => $pagination));
        }
        return $this->render('GdsVenteBundle:SessionCaisse:list.html.twig', array('pagination' => $pagination));
    }

    /**
     * @Secure(roles="ROLE_ADMIN")
     */
    public function newAction() {
        $sessionCaisse = new SessionCaisse();
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $sessionCaisse->setDate(new \DateTime());
        $form = $this->createForm(new SessionCaisseType(), $sessionCaisse);
        if ($request->isMethod('post')) {
            $form->bind($request);
            if ($form->isValid()) {
                $sessionCaisse = $form->getData();
                $caisse = $sessionCaisse->getCaisse();
                $caisse->setIsActive(true);
                $user = $sessionCaisse->getUser();
                $user->setIsCaissier(true);
                $em->persist($user);
                $em->persist($caisse);
                $em->persist($sessionCaisse);
                $em->flush();
                $red = $request->get('red');
                if ($red == 'n') {
                    $sessionCaisse = new SessionCaisse();
                    $sessionCaisse->setDate(new \DateTime());
                    $form = $this->createForm(new SessionCaisseType(), $sessionCaisse);
                    return $this->render('GdsVenteBundle:SessionCaisse:new.html.twig', array('form' => $form->createView()));
                } else {
                    $em = $this->getDoctrine()->getEntityManager();
                    $request = $this->getRequest();
                    if ($this->getUser()->hasRole('ROLE_SUPER_ADMIN')) {
                        $dql = "SELECT s FROM GdsVenteBundle:SessionCaisse s WHERE (s.date_fermeture is NULL)";
                        $query = $em->createQuery($dql);
                    } else {
                        $dql = "SELECT s FROM GdsVenteBundle:SessionCaisse s LEFT JOIN s.caisse c WHERE (s.date_fermeture is NULL and c.departement=:departement)";
                        $query = $em->createQuery($dql)->setParameters(array('departement' => $this->getUser()->getDepartement()->getId()));
                    }

                    $paginator = $this->get('knp_paginator');
                    $pagination = $paginator->paginate(
                            $query, $request->get('page', 1)/* page number */, 10/* limit per page */
                    );
                    $pagination->setUsedRoute('gds_session-caisse_list');
                    if ($request->get('type')) {
                        return $this->render('GdsVenteBundle:SessionCaisse:pageList.html.twig', array('pagination' => $pagination));
                    }
                    return $this->render('GdsVenteBundle:SessionCaisse:index.html.twig', array('pagination' => $pagination));
                }
            }
        }
        return $this->render('GdsVenteBundle:SessionCaisse:new.html.twig', array('form' => $form->createView()));
    }

    /**
     * @Secure(roles="ROLE_SUPER_ADMIN")
     */
    public function deleteAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        $caisse = $em->getRepository('GdsVenteBundle:Caisse')->find($id);
        $caisse->setIsSupprimer(true);
        $em->persist($caisse);
        $em->flush();
        $dql = "SELECT c FROM GdsVenteBundle:Caisse c WHERE (c.is_supprimer=:is_supprimer and c.libelle LIKE :libelle and c.code LIKE :code)";
        $query = $em->createQuery($dql)->setParameters(array('is_supprimer' => false, 'libelle' => '%' . $request->get('libelle') . '%', 'code' => '%' . $request->get('code') . '%'));
        $paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
                $query, $request->get('page', 1)/* page number */, 10/* limit per page */
        );
        $pagination->setUsedRoute('gds_caisse_list');
        if ($request->get('type')) {
            return $this->render('GdsVenteBundle:Caisse:pageList.html.twig', array('pagination' => $pagination));
        }
        return $this->render('GdsVenteBundle:Caisse:list.html.twig', array('pagination' => $pagination));
    }

    /**
     * @Secure(roles="ROLE_ADMIN")
     */
    public function afficheAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        $sessionCaisse = $em->getRepository('GdsVenteBundle:SessionCaisse')->find($id);
        $dql = "SELECT t FROM GdsVenteBundle:TicketCaisse t WHERE (t.session =:sessionCaisse)";
        $query = $em->createQuery($dql)->setParameter('sessionCaisse', $sessionCaisse);
        $paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
                $query, $request->get('page', 1)/* page number */, 10/* limit per page */
        );
        if ($request->get('type') == 'ajax') {
            return $this->render('GdsVenteBundle:sessionCaisse:page_liste_detail.html.twig', array('sessionCaisse' => $sessionCaisse, 'pagination' => $pagination));
        }
        return $this->render('GdsVenteBundle:sessionCaisse:affiche.html.twig', array('sessionCaisse' => $sessionCaisse, 'pagination' => $pagination));
    }

    /**
     * @Pdf()
     */
    public function imprimeTicketAction() {
        $format = $this->get('request')->get('_format');
        return $this->render('GdsVenteBundle:Caisse:helloAction.pdf.twig');
    }

    public function pointVenteAction() {
        return $this->render(sprintf('GdsVenteBundle:Caisse:pointVente.html.twig'));
    }

}
